<?php
namespace app\api\controller\v2\controller\dynamic;

use app\api\controller\v2\model\dynamic\Dynamic as Dy;
use think\Controller;

use app\api\controller\Api;
use think\Model;
use think\Db;
use think\Image;
use think\Request;
use JPush\Client as JPush;
use think\Response;
use app\api\controller\UnauthorizedException;
use think\Validate;



/**
 *
 */
class Dynamic extends api
{
    //个人动态发表
    public function create_dynamic($user_id,$latitude,$longitude,$user_address,$content){
        $time = date('Y-m-d H:i:s');
        if (!empty($user_address)) {
            $data = ['user_id'=>$user_id,'content'=>$content,'latitude'=>$latitude,'longitude'=>$longitude,'user_address'=>$user_address,'send_time'=>$time];
            $dynamic_id = Db::table('ym_user_dynamic')->insertGetId($data);
            Db::query("insert into ym_user_dynamic_upvoteCount values(null,'$dynamic_id',0)");
        }else{
            $data = ['user_id'=>$user_id,'content'=>$content,'latitude'=>null,'longitude'=>null,'user_address'=>null,'send_time'=>$time];
            $dynamic_id = Db::table('ym_user_dynamic')->insertGetId($data);
            Db::query("insert into ym_user_dynamic_upvoteCount values(null,'$dynamic_id',0)");
        }
        $files = Request::instance()->file('img');
        foreach($files as $file){
            $info = $file->move(ROOT_PATH . 'public' . DS. 'uploads'.DS.'user_dynamic_img');
            if ($info) {
                $path = '/uploads/user_dynamic_img/' . $info->getSaveName();
                $path = str_replace('\\', '/', $path);

                Db::query("insert into ym_user_dynamic_img values(null,'$dynamic_id','$path')");
            }
        }
        return $this->returnmsg(200,"发表动态成功");
    }

//个人动态显示
    public function show_dynamic_img($user_id,$pageno){
        $result = Dy::getShowDynamicImg($user_id,$pageno);
        return $this->returnmsg(200,"动态图片",$result);
    }

    //显示关注人动态
    public function follow_user_dynamic($user_id,$pageno){
        $pagesize = 3;
        $startno = $pageno *$pagesize;
        $str = "";
        $ids = Db::query("select follow_id from ym_user_follow where user_id = '$user_id'");
        // var_dump($ids);
        if (!empty($ids)) {

            $length = count($ids);
            //for ($i=0; $i < $length; $i++) {
            //     $follow_id = $ids[$i]['follow_id'];
            //    $str=','.$follow_id;
            //}
            //$follow_ids = substr($str,1);
            for ($i=0; $i < $length; $i++) {
                $follow_id = $ids[$i]['follow_id'];
                if($i == 0){
                    $str.=$follow_id;
                }
                else{
                    $str.=','.$follow_id;
                }
            }
            $follow_ids = $str;
            $dynamic = Db::query("select b.user_id,b.user_name,b.user_icon,a.dynamic_id,a.content,a.latitude as post_latitude,a.longitude as post_longitude,a.user_address,a.send_time from ym_user_dynamic as a left join user_info as b on a.user_id=b.user_id  where a.user_id in($follow_ids) ORDER BY a.send_time DESC limit {$startno},{$pagesize}");
            //join ym_user_dynamic_upvoteCount as c on a.dynamic_id = c.dynamic_id
            $length = count($dynamic);
            for ($i=0; $i < $length; $i++) {
                $dynamic_id = $dynamic[$i]['dynamic_id'];
                $img = Db::query("select img_url from ym_user_dynamic_img where dynamic_id = '$dynamic_id'");
                $dynamic[$i]['img_urls'] = $img;
                $upvoteCount = Db::query("select upvoteCount from ym_user_dynamic_upvoteCount where dynamic_id = '$dynamic_id'");
                // var_dump($upvoteCount);
                $dynamic[$i]['upvoteCount'] = $upvoteCount[0]['upvoteCount'];
                $count = Db::query("select count(*) as count from ym_user_dynamic_cmmt where dynamic_id = '$dynamic_id'");
                $dynamic[$i]['cmmt_count'] = $count[0]['count'];
                $upvote = Db::query("select upvote from ym_user_dynamic_upvote where dynamic_id = '$dynamic_id' and user_id = '$user_id'");
                if (is_null($upvote[0]['upvote'])) {
                    $dynamic[$i]['upvote'] = 0;
                }else{
                    $dynamic[$i]['upvote'] = $upvote[0]['upvote'];
                }
            }
            return $this->returnmsg(200,'关注人动态',$dynamic);
        }else{//没有关注的人
            return $this->returnmsg(200,'你还没有关注的人哦');
        }
    }



    //个人动态，评论(一级)
    public function dynamic_cmmt($dynamic_id,$user_id,$super_user_id,$super_cmmt_id,$cmmt_content){
        $time = date('Y-m-d H:i:s');
        $post = Db::query("select user_id from ym_user_dynamic where dynamic_id = '$dynamic_id'");
        $be_user_id = $post[0]['user_id'];
        $img = Db::query("select img_url from ym_user_dynamic_img where dynamic_id = '$dynamic_id' limit 1");
        $img_url = $img[0]['img_url'];

        if (!empty($super_user_id) && !empty($super_cmmt_id)) {

            $data = ['dynamic_id'=>$dynamic_id,'user_id'=>$user_id,'super_user_id'=>$super_user_id,'super_cmmt_id'=>$super_cmmt_id,'cmmt_content'=>$cmmt_content,'create_time'=>$time];
            $cmmt_id = Db::table('ym_user_dynamic_cmmt')->insertGetId($data);

            Db::query("insert into ym_user_dynamic_cmmt_upvoteCount values(null,'$cmmt_id','$dynamic_id',0)");

            $message_data = ['be_user_id'=>$super_user_id,'user_id'=>$user_id,'message_type'=>4,'create_time'=>$time];
            $m_id = Db::table('ym_message')->insertGetId($message_data);
            $reply_data = ['m_id'=>$m_id,'module_type'=>1,'id'=>$dynamic_id,'cmmt_id'=>$cmmt_id,'cmmt_content'=>$cmmt_content,'img_url'=>$img_url];
            $m_reply_id = Db::table('ym_message_reply')->insertGetId($reply_data);

            $user_name = Db::query("select user_name from user_info where user_id = '$user_id'");
            $name = $user_name[0]['user_name'];

            $this->push($super_user_id,$name.'回复了你的动态,'.$cmmt_content);
            return $this->returnmsg(200,'回复成功');
        }else{
            $data = ['dynamic_id'=>$dynamic_id,'user_id'=>$user_id,'super_user_id'=>null,'super_cmmt_id'=>null,'cmmt_content'=>$cmmt_content,'create_time'=>$time];
            $cmmt_id = Db::table('ym_user_dynamic_cmmt')->insertGetId($data);

            Db::query("insert into ym_user_dynamic_cmmt_upvoteCount values(null,'$cmmt_id','$dynamic_id',0)");

            //推送消息
            $message_data = ['be_user_id'=>$be_user_id,'user_id'=>$user_id,'message_type'=>3,'create_time'=>$time];
            $m_id = Db::table('ym_message')->insertGetId($message_data);
            $cmmt_data = ['m_id'=>$m_id,'module_type'=>1,'id'=>$dynamic_id,'cmmt_id'=>$cmmt_id,'content'=>$cmmt_content,'img_url'=>$img_url];
            $m_cmmt_id = Db::table('ym_message_cmmt')->insertGetId($cmmt_data);

            $user_name = Db::query("select user_name from user_info where user_id = '$user_id'");
            $name = $user_name[0]['user_name'];
            // $message = '关注了你';
            $be_user_id = strval($be_user_id);
            $this->push($be_user_id,$name.'评论了你的动态,'.$cmmt_content);
            return $this->returnmsg(200,'发表评论成功');
        }
    }

    //个人动态点赞
    public function dynamic_upvote($dynamic_id,$user_id){

        $time = date('Y-m-d H:i:s');
        $dynamic = Db::query("select user_id 
                            from ym_user_dynamic 
                            where dynamic_id = '$dynamic_id'");
        $be_user_id = $dynamic[0]['user_id'];
        $img = Db::query("select img_url 
                            from ym_user_dynamic_img 
                            where dynamic_id = '$dynamic_id' limit 1");
        $img_url = $img[0]['img_url'];

        $result = Db::query("select upvote 
                            from ym_user_dynamic_upvote 
                            where dynamic_id = '$dynamic_id'");
        $array = Db::query(" select count(*)
                             from ym_user_dynamic_upvote 
                             where dynamic_id = '$dynamic_id'");
        $count = $array[0]['count(*)'];
        if (empty($result)) {

            Db::query("insert into ym_user_dynamic_upvote values(null,'$dynamic_id','$user_id',1)");
            Db::query("update ym_user_dynamic_upvoteCount set upvoteCount = 1 where dynamic_id = '$dynamic_id'");

            //推送消息
            $message_data = ['be_user_id'=>$be_user_id,'user_id'=>$user_id,'message_type'=>2,'create_time'=>$time];
            $m_id = Db::table('ym_message')->insertGetId($message_data);
            $upvote_data = ['m_id'=>$m_id,'module_type'=>1,'id'=>$dynamic_id,'img_url'=>$img_url];
            $m_upvote_id = Db::table('ym_message_upvote')->insertGetId($upvote_data);

            $user_name = Db::query("select user_name from user_info where user_id = '$user_id'");
            $name = $user_name[0]['user_name'];
            // $message = '关注了你';
            $be_user_id = strval($be_user_id);
            $this->push($be_user_id,$name.'赞了你的动态');
            return $this->returnmsg(200,"点赞成功");
        }else{
            Db::query("insert into ym_user_dynamic_upvote values(null,'$dynamic_id','$user_id',1)");
            Db::query("update ym_user_dynamic_upvoteCount set upvoteCount = '$count' + 1 where dynamic_id = '$dynamic_id'");

            //推送消息
            $message_data = ['be_user_id'=>$be_user_id,'user_id'=>$user_id,'message_type'=>2,'create_time'=>$time];
            $m_id = Db::table('ym_message')->insertGetId($message_data);
            $upvote_data = ['m_id'=>$m_id,'module_type'=>1,'id'=>$dynamic_id,'img_url'=>$img_url];
            $m_upvote_id = Db::table('ym_message_upvote')->insertGetId($upvote_data);

            $user_name = Db::query("select user_name from user_info where user_id = '$user_id'");
            $name = $user_name[0]['user_name'];
            // $message = '关注了你';
            $be_user_id = strval($be_user_id);
            $this->push($be_user_id,$name.'赞了你的动态');
            return $this->returnmsg(200,"点赞成功");
        }
    }

//显示个人动态评论
    public function show_dynamic_cmmt($user_id,$dynamic_id,$pageno){
        $result = DY::get_show_dynamic_cmmt($user_id,$dynamic_id,$pageno);
        return $this->returnmsg(200,'个人动态评论',$result);


    }

    //个人动态的评论点赞
    public function dynamic_cmmt_upvote($dynamic_id,$cmmt_id,$user_id){

        $time = date('Y-m-d H:i:s');
        $dynamic = Db::query("select user_id from ym_user_dynamic where dynamic_id = '$dynamic_id'");
        $be_user_id = $dynamic[0]['user_id'];

        $img = Db::query("select img_url from ym_user_dynamic_img where dynamic_id = '$dynamic_id' limit 1");
        $img_url = $img[0]['img_url'];


        $result = Db::query("select cmmt_upvote from ym_user_dynamic_cmmt_upvote where dynamic_id = '$dynamic_id' and cmmt_id = '$cmmt_id'");
        $array = Db::query(" select cmmt_upvoteCount from ym_user_dynamic_cmmt_upvoteCount where dynamic_id = '$dynamic_id' and cmmt_id = '$cmmt_id'");
        $count = $array[0]['cmmt_upvoteCount'];
        if (empty($result)) {
            Db::query("insert into ym_user_dynamic_cmmt_upvote values(null,'$cmmt_id','$dynamic_id','$user_id',1)");
            Db::query("update ym_user_dynamic_cmmt_upvoteCount set cmmt_upvoteCount = 1 where dynamic_id = '$dynamic_id' and cmmt_id = '$cmmt_id'");

            //推送消息
            $message_data = ['be_user_id'=>$be_user_id,'user_id'=>$user_id,'message_type'=>2,'create_time'=>$time];
            $m_id = Db::table('ym_message')->insertGetId($message_data);
            $upvote_data = ['m_id'=>$m_id,'module_type'=>1,'id'=>$dynamic_id,'cmmt_id'=>$cmmt_id,'img_url'=>$img_url];
            $m_upvote_id = Db::table('ym_message_upvote')->insertGetId($upvote_data);

            // Db::query("insert into ym_message_upvote_dynamic values(null,'$m_upvote_id','$dynamic_id','$cmmt_id','$img_url')");
            $user_name = Db::query("select user_name from user_info where user_id = '$user_id'");
            $name = $user_name[0]['user_name'];
            // $message = '关注了你';
            $be_user_id = strval($be_user_id);
            $this->push($be_user_id,$name.'赞了你的动态,');
            return $this->returnmsg(200,"点赞成功");
        }else{
            Db::query("insert into ym_user_dynamic_cmmt_upvote values(null,'$cmmt_id','$dynamic_id','$user_id',1)");
            Db::query("update ym_user_dynamic_cmmt_upvoteCount set cmmt_upvoteCount = '$count' + 1 where dynamic_id = '$dynamic_id' and cmmt_id = '$cmmt_id'");

            //推送消息
            $message_data = ['be_user_id'=>$be_user_id,'user_id'=>$user_id,'message_type'=>2,'create_time'=>$time];
            $m_id = Db::table('ym_message')->insertGetId($message_data);
            $upvote_data = ['m_id'=>$m_id,'module_type'=>1,'id'=>$dynamic_id,'cmmt_id'=>$cmmt_id,'img_url'=>$img_url];
            $m_upvote_id = Db::table('ym_message_upvote')->insertGetId($upvote_data);


            $user_name = Db::query("select user_name from user_info where user_id = '$user_id'");
            $name = $user_name[0]['user_name'];
            // $message = '关注了你';
            $be_user_id = strval($be_user_id);
            $this->push($be_user_id,$name.'赞了你的动态');
            return $this->returnmsg(200,"点赞成功");
        }
    }

    //删除个人动态
    public function delete_user_dynamic($dynamic_id){
        Db::query("delete from ym_user_dynamic where dynamic_id = '$dynamic_id'");
        $result = Db::query("select *from ym_user_dynamic where dynamic_id = '$dynamic_id'");
        if (empty($result)) {
            Db::query("delete from ym_user_dynamic_img where dynamic_id = '$dynamic_id'");
            Db::query("delete from ym_user_dynamic_upvote where dynamic_id = '$dynamic_id'");
            Db::query("delete from ym_user_dynamic_upvoteCount where dynamic_id = '$dynamic_id'");
            Db::query("delete from ym_user_dynamic_cmmt where dynamic_id = '$dynamic_id'");
            Db::query("delete from ym_user_dynamic_cmmt_upvote where dynamic_id = '$dynamic_id'");
            Db::query("delete from ym_user_dynamic_cmmt_upvoteCount where dynamic_id = '$dynamic_id'");
            return $this->returnmsg(200,"删除成功");
        }else{
            return $this->returnmsg(400,"删除失败");
        }
    }



    //个人动态,删除文章评论12-22
    public function delete_dynamic_cmmt($dynamic_id,$cmmt_id){

        Db::query("delete from ym_user_dynamic_cmmt 
                    where dynamic_id = '$dynamic_id' and cmmt_id = '$cmmt_id'");
        $result = Db::query("select *
                              from ym_user_dynamic_cmmt 
                              where dynamic_id = '$dynamic_id' and cmmt_id = '$cmmt_id'");
        if (empty($result)) {
            Db::query("delete from ym_user_dynamic_cmmt_upvote where dynamic_id = '$dynamic_id' and cmmt_id = '$cmmt_id'");
            Db::query("delete from ym_user_dynamic_cmmt_upvoteCount where dynamic_id = '$dynamic_id' and cmmt_id = '$cmmt_id'");
            return $this->returnmsg(200,"删除成功");
        }else{
            return $this->returnmsg(400,"删除失败");
        }
    }


    //关注&取消关注
    public function user_follow($user_id,$follow_id){
        $follow_time = date("Y-m-d H:i:s");
        if ($user_id == $follow_id) {
            return $this->returnmsg(401,'不能自己关注自己的哦');
        }else{
            $result = Db::query("select *from ym_user_follow where user_id = '$user_id' and follow_id = '$follow_id'");
            if (!empty($result)) {//已经关注
                //取消关注
                Db::table('ym_user_follow')->where('user_id',$user_id)->where('follow_id',$follow_id)->delete();
                $follow = Db::query("select follow_count from user_info where user_id = '$user_id'");
                $follow_count = $follow[0]['follow_count'];
                $fans = Db::query("select fans_count from user_info where user_id = '$follow_id'");
                $fans_count = $fans[0]['fans_count'];
                if ($follow_count != 0 && $follow_count >0) {
                    Db::query("update user_info set follow_count = '$follow_count' - 1 where user_id = '$user_id'");
                }
                if ($fans_count != 0 && $fans_count >0) {
                    Db::query("update user_info set fans_count = '$fans_count' - 1 where user_id = '$follow_id'");
                }
                return $this->returnmsg(200,'取消关注成功');
            }else{
                //关注
                $data = ['user_id'=>$user_id,'follow_id'=>$follow_id,'follow_time'=>$follow_time];
                Db::table('ym_user_follow')->insert($data);

                //插入消息表
                $message_data = ['be_user_id'=>$follow_id,'user_id'=>$user_id,'message_type'=>0,'create_time'=>$follow_time];
                $m_id = Db::table('ym_message')->insertGetId($message_data);
                Db::query("insert into ym_message_follow values(null,'$m_id','$follow_id','$user_id')");

                $follow = Db::query("select follow_count from user_info where user_id = '$user_id'");
                $follow_count = $follow[0]['follow_count'];
                $fans = Db::query("select fans_count from user_info where user_id = '$follow_id'");
                $fans_count = $fans[0]['fans_count'];
                Db::query("update user_info set follow_count = '$follow_count' + 1 where user_id = '$user_id'");
                Db::query("update user_info set fans_count = '$fans_count' + 1 where user_id = '$follow_id'");

                $user_name = Db::query("select user_name from user_info where user_id = '$user_id'");
                $name = $user_name[0]['user_name'];
                // $message = '关注了你';
                $this->push($follow_id,$name.'关注了你');
                return $this->returnmsg(200,'关注成功');
            }
        }
    }

    //显示主页
    public function show_homepage($user_id,$other_user_id){
        $str = '';
        if ($user_id != $other_user_id) {
            $result = Db::query("select b.user_id,b.user_name,b.user_icon,b.user_desc,b.background_img,c.pet_food,b.follow_count,b.fans_count from user_info as b join ym_pet_food as c on b.user_id = c.user_id where b.user_id = '$other_user_id' ");

            //判断是否已关注
            $follow_or_not = Db::query("select *from ym_user_follow where user_id = '$user_id' and follow_id = '$other_user_id'");

            $dynamic_ids = Db::query("select dynamic_id from ym_user_dynamic where user_id = '$other_user_id'");
            if (empty($dynamic_ids)) {
                $result[0]['upvoteCount'] = 0;
            }else{
                $length = count($dynamic_ids);
                for ($i=0; $i < $length; $i++) {
                    $dynamic_id = $dynamic_ids[$i]['dynamic_id'];
                    $str.=','.$dynamic_id;
                }
                $ids = substr($str,1);

                $upvoteCount = Db::query("select sum(upvoteCount) as count from ym_user_dynamic_upvoteCount where dynamic_id in($ids)");

                $result[0]['upvoteCount'] = $upvoteCount[0]['count'];
            }
            if (empty($follow_or_not)) {
                $result[0]['follow_status'] = 0;
            }else{
                $result[0]['follow_status'] = 1;
            }
            return $this->returnmsg(200,'别人首页头部',$result);

        }else{
            //自己首页
            $result = Db::query("select b.user_id,b.user_name,b.user_icon,b.user_desc,b.background_img,c.pet_food,b.follow_count,b.fans_count from user_info as b join ym_pet_food as c on b.user_id = c.user_id where b.user_id = '$user_id' ");

            $dynamic_ids = Db::query("select dynamic_id from ym_user_dynamic where user_id = '$other_user_id'");
            if (empty($dynamic_ids)) {
                $result[0]['upvoteCount'] = 0;
            }else{
                $length = count($dynamic_ids);
                for ($i=0; $i < $length; $i++) {
                    $dynamic_id = $dynamic_ids[$i]['dynamic_id'];
                    $str.=','.$dynamic_id;
                }
                $ids = substr($str,1);
                $upvoteCount = Db::query("select sum(upvoteCount) as count from ym_user_dynamic_upvoteCount where dynamic_id in($ids)");

                $result[0]['upvoteCount'] = $upvoteCount[0]['count'];
            }
            return $this->returnmsg(200,'个人首页头部',$result);
        }
    }


//显示用户个人动态
    public function show_user_dynamic($dynamic_id,$user_id){
       $dynamic = Dy::getShowUserDynamic($dynamic_id,$user_id);
        return $this->returnmsg(200,"dynamic",$dynamic);
    }

    /**
     * 最新显示用户个人动态
     * @param $user_id
     * @param $page
     * @param $num
     */
    public function show_user_dynamic_paging($user_id, $page, $num = 10)
    {

        $dynamic = Dy::getShowUserDynamicPading($user_id,$page*$num, $num);
        if(!$dynamic['status'])
        {
            return $this->returnmsg(400,"problem");
        }
        return $this->returnmsg(200,"dynamic",$dynamic['data']);
    }








    //推送全部
    public function push($user_id,$message){
        $appkey = 'a3d1bad973276db7cee3f40a';
        $master_secret = 'ac5c6c6b1101643082c88913';
        $client = new JPush($appkey,$master_secret);
        $r_id = Db::query("select registration_id from ym_device_no where user_id = '$user_id'");
        $registration_id = $r_id[0]['registration_id'];

        $push = $client->push();
        $push->setPlatform('all');
        //别名
        $push->addAlias($user_id);
        //设备号
        $push->addRegistrationId($registration_id);
        // $push->iosNotification();
        $push->iosNotification($message, [
            'sound' => 'sound.cat',
            'badge' => '+1',

        ]);
        $push->options(array(
            "apns_production" => true
        ));
        // try {
        $push->send();

    }








}